var config = require('../../config');
var OAuth = require('wechat-oauth');
var jsapiTicketController = require('./JsapiTicketController');
var sign = require('../../common/utils/sign.js');
var cinemaMenuModel = require('../models/CinemaMenuModel');

var client = new OAuth(config.app.appid, config.app.appsecret);
var queryConfig = function (callback, req) {
    var url = req.protocol + '://' + req.host + req.originalUrl; //获取当前url
    var ret = sign(jsapiTicketController.getJsapiTicket(), url);
    console.log(ret);
    callback(ret);
};


module.exports = {

    /**
     * 自定菜单---影吧
     */
    getCinemaHome: function (req, res) {
        //test cookie openid start
        var cookie_openid;
        if(req.signedCookies.openid){
            cookie_openid = req.signedCookies.openid;
            queryConfig(function (ret) {
                res.render('cinemahome', {
                    title: '点播影院',
                    layout: false,
                    sign_pkg: ret.jsapi_ticket,
                    appId: config.app.appid,
                    timestamp: ret.timestamp,
                    nonceStr: ret.nonceStr,
                    signature: ret.signature,
                    open_id: cookie_openid,
                    sdkKey:config.amap.sdkKey
                    // ios: req.ua.os === 'iOS'
                });
            }, req);
        }else{
            client.getAccessToken(req.query.code, function (err, result) {
                var openid;
                if(err){
                    res.render('error', {
                        title: '服务器出错',
                        message:'请求open_id失败',
                        layout: false
                    });
                }else {
                    openid = result.data.openid;
                queryConfig(function (ret) {

            res.cookie('openid', openid, {maxAge:1000*60*60*24,signed:true});


            res.render('cinemahome', {
                title: '点播影院',
                layout: false,
                sign_pkg: ret.jsapi_ticket,
                appId: config.app.appid,
                timestamp: ret.timestamp,
                nonceStr: ret.nonceStr,
                signature: ret.signature,
                // open_id: openid,
                sdkKey:config.amap.sdkKey
                // ios: req.ua.os === 'iOS'
            });
        }, req);
                }
            })
        }
    },
//影吧页面---获取影吧信息
                getCinemaInfo: function (req, res) {
                    cinemaMenuModel.getCinemaInfoLimitModel(req, function (data) {
                        res.json(data);
                    });
                } ,
                getCinema:function (req, res) {

                    var mid = '';
                    if (req.query.mid) {
                        mid = req.query.mid;
                    }
                    res.render('cinema', {
                        layout: false,
                        mid: mid
                    })
                }


            };
